<template>
  <div>
    <!-- Form -->

    <el-dialog
      :title="info.isAdd ? '添加' : '修改'"
      :visible.sync="info.isshow"
      @close="close"
    >
      <el-form :model="form">
        <el-form-item label="上级分类" :label-width="formLabelWidth">
          <el-select v-model="form.pid" placeholder="请选择上级分类">
            <el-option label="顶级分类" :value="0"></el-option>
            <!-- 4.6 遍历arr  -->
            <el-option
              v-for="item in arr"
              :key="item.id"
              :label="item.catename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="分类名称" :label-width="formLabelWidth">
          <el-input v-model="form.catename" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item v-if="form.pid!=0" label="上传图片" :label-width="formLabelWidth" >
          <el-upload
            class="avatar-uploader"
            action="#"
            :show-file-list="false"
            :on-change="changeImg"
            
          >
            <img v-if="imgUrl" :src="imgUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>

        <el-form-item>
          <el-switch
            v-model="form.status"
            inactive-text="状态"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <el-button type="primary" @click="add">{{
        info.isAdd ? "添加" : "修改"
      }}</el-button>
    </el-dialog>
  </div>
</template>

<script>
import { reqcateadd, reqcateinfo, reqcateedit, reqcatelist } from "../../../http/api";
import { successAlert, errorAlert } from "../../../utils/alert";
import path from 'path'
export default {
  props: ["info"],
  data() {
    return {
      form: {
        pid: "",
        catename: "",
        status: 1,  
        img:''
      },
      imgUrl: "",
      arr:[],
      treeData: [],
      dialogFormVisible: true,

      formLabelWidth: "120px",
      // dialogImageUrl: "",
      dialogVisible: false,
    };
  },
  mounted() {
    //请求分类上级列表
    reqcatelist({istree:true}).then(res=>{
      if(res.data.code==200){
        this.arr=res.data.list;
      }
    })
  },
  methods: {
    //初始化form
    setForm() {
      this.form = {
        pid: "",
        catename: "",
        status: 1,  
        img:''
      };
      this.imgUrl='';
    },
    //请求列表数据
    add() {
      if (this.info.isAdd) {
        //先获取树的所选id
        reqcateadd(this.form).then((res) => {
          if (res.data.code == 200) {
            //弹成功
            successAlert(res.data.msg);
            //清空form
            this.setForm();
            //弹框消失
            this.info.isshow = false;
            //列表刷新
            this.$emit("init");
          }
        });
      } else {
        reqcateedit(this.form).then((res) => {
          if (res.data.code == 200) {
            //弹成功
            successAlert(res.data.msg);
            //清空form
            this.setForm();
            //弹框消失
            this.info.isshow = false;
            //列表刷新
            this.$emit("init");
          }
        });
      }
    },
    //请求一条要修改的数据
    getOne(id) {
      reqcateinfo({ id: id }).then((res) => {
        if (res.data.code == 200) {
          this.form = res.data.list;
          //此刻form没有id,补id
          this.form.id = id;
          //给图片赋值
          this.imgUrl=this.$pre+res.data.list.img
        }
      });
    },

    //如果是编辑的蒙层关闭了 清空form
    close() {
      if (this.info.isAdd == false) {
        this.setForm();
      }
    },
    //上传图片
    changeImg(file) {
      //判断上传文件是否是图片
      let format = ['jpg','gif','tga','svg','webp','png','bmp'];
      let imgName = format.includes(path.extname(file.raw.name).split('.')[1]);
      if(!imgName){
        errorAlert("请上传正确的文件格式");
        ruturn;
      }
      //判断上传文件大小 大于2m不予上传
      if(file.size>5*1024*1024){
        errorAlert("请上传小于2m的图片");
        ruturn;
      }
      this.imgUrl = URL.createObjectURL(file.raw);
      this.form.img=file.raw;
    },
  },
};
</script>

<style>
.el-switch {
  margin-left: 80px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
